package com.guaju.yueshangLife.activity;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.OrderListAdapter;
import com.guaju.yueshangLife.adapter.OrderPagerAdapter;
import com.guaju.yueshangLife.base.BaseFragmentActivity;
import com.guaju.yueshangLife.databinding.ActivityOrderCenterBinding;
import com.guaju.yueshangLife.fragment.order.OrderListFragment;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.net.NetworkUtil;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.widget.OrderTab;

import java.util.ArrayList;

public class OrderListActivity extends BaseFragmentActivity {

    public static final String REQUEST_REASON_SELECTED = "cancle_reason";
    public static final String REQUEST_REASON = "reason";
    public static final String ORDER_ID = "orderId";
    public int selectedReason = -1;
    private static final int REQUEST_SELECT_REASON = 1800;
    String[] types = {"all", "pay", "ship", "comment", "cancel", "refund"};


    private ActivityOrderCenterBinding binding;
    public static final String TABTYPE = "tabtype";
    public static final String TAB_TOTAL = "total";
    public static final String TAB_UNPAY = "unpay";
    public static final String TAB_UNRECEIVED = "unreceived";
    public static final String TAB_UNCOMMENT = "uncomment";
    public static final String TAB_CANCEL = "cancel";
    private ArrayList<OrderListFragment> fragmentList = new ArrayList();
    private OrderTab[] tabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_center);
        binding.topbar.tvRight.setVisibility(View.INVISIBLE);
        tabs = new OrderTab[]{binding.tabTotal, binding.tabUnpay, binding.tabUnreceived, binding.tabUncomment, binding.tabCanceled};
        if (NetworkUtil.isConnected(OrderListActivity.this)) {
            initView();
            setListener();
            Intent intent = getIntent();
            if (intent != null) {
                Bundle bundleExtra = intent.getBundleExtra(TABTYPE);
                if (bundleExtra != null) {
                    String type = bundleExtra.getString(TABTYPE);
                    if (TextUtils.equals(type, TAB_TOTAL)) {
                        binding.viewpager.setCurrentItem(0);
                    } else if (TextUtils.equals(type, TAB_UNPAY)) {
                        binding.viewpager.setCurrentItem(1);
                    } else if (TextUtils.equals(type, TAB_UNRECEIVED)) {
                        binding.viewpager.setCurrentItem(2);
                    } else if (TextUtils.equals(type, TAB_UNCOMMENT)) {
                        binding.viewpager.setCurrentItem(3);
                    } else if (TextUtils.equals(type, TAB_CANCEL)) {
                        binding.viewpager.setCurrentItem(4);
                    }

                }
            }
        } else {
            showNetError();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initView() {
        for (int i = 0; i < 5; i++) {
            OrderListFragment instance = OrderListFragment.getInstance(types[i] + "");
            fragmentList.add(instance);
        }
        FragmentManager sfm = getSupportFragmentManager();
        OrderPagerAdapter orderPagerAdapter = new OrderPagerAdapter(sfm, fragmentList);
        binding.viewpager.setOffscreenPageLimit(fragmentList.size());
        binding.viewpager.setAdapter(orderPagerAdapter);
    }


    private void setListener() {
        setTabClickListener(binding.tabUnpay);
        setTabClickListener(binding.tabCanceled);
        setTabClickListener(binding.tabTotal);
        setTabClickListener(binding.tabUncomment);
        setTabClickListener(binding.tabUnreceived);
        setViewPagerChangeListener();

    }

    private void setViewPagerChangeListener() {
        binding.viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setUnSelectedAll();
                tabs[i].setSelected();
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void setTabClickListener(final OrderTab orderTab) {
        orderTab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setUnSelectedAll();
                orderTab.setSelected();
                if (orderTab == binding.tabTotal) {
                    binding.viewpager.setCurrentItem(0);
                } else if (orderTab == binding.tabUnpay) {
                    binding.viewpager.setCurrentItem(1);
                } else if (orderTab == binding.tabUnreceived) {
                    binding.viewpager.setCurrentItem(2);
                } else if (orderTab == binding.tabUncomment) {
                    binding.viewpager.setCurrentItem(3);
                } else if (orderTab == binding.tabCanceled) {
                    binding.viewpager.setCurrentItem(4);
                }
            }
        });
    }

    private void setUnSelectedAll() {
        binding.tabUnpay.setNormal();
        binding.tabCanceled.setNormal();
        binding.tabTotal.setNormal();
        binding.tabUncomment.setNormal();
        binding.tabUnreceived.setNormal();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_REASON) {
            if (data != null) {
                String reason = data.getStringExtra(REQUEST_REASON);
                selectedReason = data.getIntExtra(REQUEST_REASON_SELECTED, -1);
                if (!TextUtils.isEmpty(reason)) {
                    /**
                     * 取消订单
                     */
                    int intExtra = data.getIntExtra(ORDER_ID, -1);
                    if (intExtra != -1) {
                        ApiEntity.cancleOrder(intExtra, reason);
                    }

                }
            } else {
                if (-1 == selectedReason) {
                    ToastUtil.show("未选择取消原因！");
                }
            }
        }
    }
}
